package cn.aylog.test;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Demo {

    @Test
    public void run1() throws Exception {
        String path = "mybatis.config.xml";
        InputStream is = Demo.class.getClassLoader().getResourceAsStream(path);
        SAXReader reader = new SAXReader();
        Document document = reader.read(is);
        Element rootElement = document.getRootElement();
        parseRoot(rootElement);
    }

    private void parseRoot(Element rootElement) {
        List<Element> environments = rootElement.elements("environments");
        Element element = environments.get(0);
        List<Attribute> attributes = element.attributes();
        Attribute attribute = attributes.get(0);
        String name = attribute.getName();
        Object value = attribute.getData();
        System.out.println(name + "--" + value);
        Attribute aDefault = element.attribute("default");
        System.out.println(aDefault);
    }

}
